package hashcodeequals;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author cyt
 * @create 2020-06-21 下午 11:08
 */
public class test01 {

    public static void main(String[] args) {

        ClassUser cyt = new ClassUser("cyt", 12);
        ClassUser cyt1 = new ClassUser("cyt", 12);

        System.out.println("cyt1 == cyt ? =" + cyt.equals(cyt1));

        HashMap<Object, Object> map = new HashMap<>();

        map.put(cyt, "cyt");
        map.put(cyt1, "cyt1");

        System.out.println("map size : " + map.size());

        HashSet<Object> set = new HashSet<>();

        set.add(cyt);
        set.add(cyt1);
        System.out.println("set size : " + set.size());

        System.out.println("cyt hashcode : " + cyt.hashCode());
        System.out.println("cyt1 hashcode : " + cyt1.hashCode());
    }
}

class ClassUser {
    private String name;
    private Integer age;

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + age.hashCode();
        result = 31 * result + age;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ClassUser) {
            ClassUser u = (ClassUser) obj;
            if (u.getName().equals(this.name) && u.getAge().equals(this.age)) {
                return true;
            }
        } else {
            return false;
        }
        return false;
    }

    public ClassUser() {
    }

    public ClassUser(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
